package net.orange.architecture.web;

import java.util.Comparator;

import javax.faces.model.SelectItem;

/**
 * The comparator for the select item in JSF. 
 */
public class SelectItemComparator implements Comparator {

	/* (non-Javadoc)
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
	 */
	public int compare(Object element1, Object element2) {
		if (element1 != null && element2 != null
				&& element1 instanceof SelectItem
				&& element2 instanceof SelectItem
				&& ((SelectItem) element1).getValue() != null
				&& ((SelectItem) element2).getValue() != null) {

			String lower1 = (String) ((SelectItem) element1).getValue();
			String lower2 = (String) ((SelectItem) element2).getValue();

			return lower1.compareTo(lower2);
		} else
			return 0;
	}
}
